<template>
  <el-form :model="VerbRegistryForm" status-icon :rules="rules" ref="VerbRegistryForm"
           label-width="auto">
    <h2 class="region_title">注册Verb</h2>
    <el-form-item prop="verbName" label="verb名称">
      <el-input type="text" v-model="VerbRegistryForm.verbName"
                placeholder="verb名称" v-if="this.$parent.data===null"></el-input>
      <el-input type="text" v-model="VerbRegistryForm.verbName"
                placeholder="verb名称" v-else disabled="true"></el-input>
    </el-form-item>
    <el-form-item
      v-for="(display, index) in VerbRegistryForm.displays"
      :label="'动词描述' + index"
      :key="display.key"
      :prop="'displays.' + index + '.value'"
    >
      <el-input class="input-with-select" v-model="display.value" placeholder="verb描述">
      <el-select v-model="display.k" slot="prepend" placeholder="请选择" style="width: 100px;">
        <el-option v-for="(key,value) in languageObjects" :key="key" :label="value" :value="value"></el-option>
      </el-select>
      </el-input>
      <el-button @click.prevent="removeDomain(display)">删除</el-button>
    </el-form-item>
    <el-form-item>
      <el-button @click="addDomain">新增描述</el-button>
    </el-form-item>
    <el-form-item prop="verbName" label="verb类型">
      <el-input type="text" v-model="VerbRegistryForm.type"
                placeholder="verb类型"></el-input>
    </el-form-item>
  </el-form>
</template>
<script>
  export default {
    name: "VerbRegistryForm",
    mounted: function () {
      this.getVerb();
    },
    data() {
      var valiVerbName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('verb名称不能为空'));
        } else {
          callback();
        }
      }
      var valiVerbType = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('verb类型不能为空'));
        } else {
          callback();
        }
      }
      return {
        select:'',
        languageObjects:{
          CN:1,
          EN:2
        },
        verbName: '',
        verbId:'',
        VerbRegistryForm: {
          verbName: '',
          displays: [
            {k: '',value:''}
          ]
        },
        type:'',
        rules: {
          verbName: [
            {validator: valiVerbName, trigger: 'blur'}
          ],
          type:[
            {validator: valiVerbType, trigger: 'blur'}
          ]
        }
      };
    },
    methods: {
      getVerb() {
        console.log("parent:", this.$parent.data)
        if (this.$parent.data !== null) {
          this.verbId = this.$parent.data
          this.$axios.get("/verb/find", {
            params: {
              verbId: this.verbId,
            }
          }).then(successResponse => {
            this.VerbRegistryForm = successResponse.data
          }).catch(failResponse => {
          })
        }
      },
      submitForm() {
        this.$refs['VerbRegistryForm'].validate((valid) => {
          if (valid) {
            let url = ''

            if (this.$parent.data !== null) {
              //更新
              url = '/verb/update'
            } else {
              //添加
              url = '/verb/add'
            }
            var display=[]
            this.VerbRegistryForm.displays.forEach(function(value, index){
              let str=value.k+":"+value.value
              let d={"value":str}
              display.push(d)
            })
            this.$axios
              .post(url, {
                verbName: this.VerbRegistryForm.verbName,
                displays: display,
                type:this.VerbRegistryForm.type
              })
              .then(success => {
                this.$message({
                  type: 'info',
                  message: "提交成功！"
                });
              }).catch(fail => {
              this.$message.error("提交失败")
            })
          } else {
            this.$message.error("编辑信息有误")
            return false;
          }
        });
      },
      resetForm() {
        this.$refs['VerbRegistryForm'].resetFields();
      },
      removeDomain(item) {
        var index = this.VerbRegistryForm.displays.indexOf(item)
        if (index !== -1) {
          this.VerbRegistryForm.displays.splice(index, 1)
        }
      },
      addDomain() {
        this.VerbRegistryForm.displays.push({
          k: '',value:'',
          key: Date.now()
        });
      }
    }
  }
</script>

<style scoped>
  .region_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: darkslateblue;
  }

</style>
